package com.frxuexi.park2210.vistor

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2210.R
import com.frxuexi.park2210.databinding.ActivityVistorAddBinding
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@Route(path = "/app/VistorAddActivity")
@AndroidEntryPoint
class VistorAddActivity : BaseActivity<ActivityVistorAddBinding,VistorViewModel>() {
    override fun initData() {
        vm.addVisitorSuc.observe(this){
            ToastUtils.showLong(it)
            finish()
        }
    }

    override fun initView() {

        binding.starttime.setOnClickListener {
            showDateDialog(binding.tvVistorstart)
        }

        binding.endtime.setOnClickListener {
            showDateDialog(binding.tvVistorend,{
                getDay()
            })

        }
        binding.tvVistorstart.text=vm.getDate(System.currentTimeMillis())
        binding.tvVistorend.text=vm.getDate(System.currentTimeMillis())
        getDay()

        binding.btnAddvistor.setOnClickListener {
            val map = mutableMapOf<String, Any>()
            map["carNum"]=binding.tvCardno.text.toString()
            map["userId"]=SPUtils.getInstance().getInt(Const.UID)
            map["visitorDescribe"]=binding.tvDesc.text.toString()
            map["visitorEnd"]=binding.tvVistorend.text.toString()
            map["visitorStart"]=binding.tvVistorstart.text.toString()
            map["visitorTime"]=binding.tvDay.text.toString()
            vm.addVisitor(map)
        }
    }

    fun getDay(){
        val start = SimpleDateFormat("yyyy-MM-dd").parse(binding.tvVistorstart.text.toString()).time
        val end = SimpleDateFormat("yyyy-MM-dd").parse(binding.tvVistorend.text.toString()).time

        val time=end-start
        val day=time/1000/60/60/24
        binding.tvDay.text="${day+1}天"
        Log.i("===",time.toString())
    }


}